package com.softWang.system.controller;

import com.softWang.common.aop.annotation.SysOperationLog;
import com.softWang.common.response.PageBean;
import com.softWang.common.request.Idparam;
import com.softWang.common.request.Keyparam;
import com.softWang.common.response.ResponseResult;
import com.softWang.system.constants.SysModel;
import com.softWang.system.controller.vo.systemDictData.*;
import com.softWang.system.po.SystemDictData;
import com.softWang.system.service.SystemDictDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;

@Validated
@RestController
@Api(value="字典项管理",tags="字典项管理")
@RequestMapping("/system/dictData")
public class SystemDictDataController {
    @Resource
    private SystemDictDataService dictDataService;

    @PreAuthorize("@ss.hasPermi('system:dictData:query')")
    @ApiOperation(value="字典数据列表(分页)")
    @RequestMapping(value = "/query", method = RequestMethod.POST)
    public ResponseResult<PageBean<SystemDictDataQueryAllResult>> queryall(@RequestBody @Validated SystemDictDataQueryAllParam param, HttpServletRequest request) {
        List<SystemDictDataQueryAllResult> results=dictDataService.queryall(param);
        PageBean page=new PageBean(results);
        return ResponseResult.success(page);
    }

    @PreAuthorize("@ss.hasPermi('system:dictData:add')")
    @ApiOperation(value="添加字典数据")
    @SysOperationLog(value = "添加字典数据", model = SysModel.DICTIONARY_DATA_MANAGE)
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    //@Repeat(message = "重复提交")
    public ResponseResult<Long> add(@RequestBody @Validated SystemDictDataAddParam param, HttpServletRequest request) throws Exception {
        SystemDictData dictData = new SystemDictData();
        BeanUtils.copyProperties(param, dictData);
        Long id = dictDataService.add(dictData);
        return ResponseResult.success(id);
    }

    @PreAuthorize("@ss.hasPermi('system:dictData:edit')")
    @ApiOperation(value="修改字典数据")
    @SysOperationLog(value = "修改字典数据", model = SysModel.DICTIONARY_DATA_MANAGE)
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public ResponseResult<Long> edit(@RequestBody @Validated SystemDictDataEditParam param, HttpServletRequest request) {
        SystemDictData dictData = new SystemDictData();
        BeanUtils.copyProperties(param, dictData);
        Long id = dictDataService.edit(dictData);
        return ResponseResult.success(id);
    }

    @PreAuthorize("@ss.hasPermi('system:dictData:detail')")
    @ApiOperation(value="查看详情")
    @RequestMapping(value = "/detail", method = RequestMethod.POST)
    public ResponseResult<SystemDictDataDetailResult> detail(@RequestBody @Validated Idparam idparam) {        ;
        SystemDictDataDetailResult result    = new SystemDictDataDetailResult();
        SystemDictData dictData = dictDataService.getById(idparam.getId());
        BeanUtils.copyProperties(dictData, result);
        return ResponseResult.success(result);
    }


    @PreAuthorize("@ss.hasPermi('system:dictData:del')")
    @ApiOperation(value="删除")
    @SysOperationLog(value = "删除字典数据", model = SysModel.DICTIONARY_DATA_MANAGE)
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    public ResponseResult del(@RequestBody @NotEmpty(message = "ids 不能为空") List<@NotNull(message = "ids 不能为空") Long> ids, HttpServletRequest request) {
        dictDataService.del(ids);
        return ResponseResult.success();
    }

    @ApiOperation(value="通过字典类型id获取字典数据列表")
    @RequestMapping(value = "/getListByDictTypeId", method = RequestMethod.POST)
    public ResponseResult<List<SystemDictDataItemResult>> getListByDictTypeId(@RequestBody @Validated Idparam idparam) {        ;
        return ResponseResult.success(dictDataService.getListByDictTypeId(idparam.getId()));
    }

    @ApiOperation(value="通过字典类型key获取字典数据列表")
    @RequestMapping(value = "/getListByDictKey", method = RequestMethod.POST)
    public ResponseResult<List<SystemDictDataItemResult>> getListByDictKey(@RequestBody @Validated Keyparam keyparam) {        ;
        return ResponseResult.success(dictDataService.getListByDictKey(keyparam.getKey()));
    }

    @ApiOperation(value="通过字典类型key和字典值获取label")
    @RequestMapping(value = "/getLabelByTypeKeyAndVal", method = RequestMethod.POST)
    public ResponseResult<String> getLabelByTypeKeyAndVal(@RequestBody @Validated SystemDictDataGetLabelByTypeKeyAndValParam keyparam) {        ;
        return ResponseResult.success(dictDataService.getLabelByTypeKeyAndVal(keyparam));
    }

    @ApiOperation(value="获取所有字典数据映射")
    @RequestMapping(value = "/dictAllData", method = RequestMethod.POST)
    public ResponseResult<List<SystemDictAllDataResult>> dictAllData() {
        List<SystemDictAllDataResult> results=dictDataService.dictAllData();
        return ResponseResult.success(results);
    }
}
